package com.design.twords.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.design.twords.R;
import com.design.twords.adatper.PharseAdapter;
import com.design.twords.adatper.RelWordAdapter;
import com.design.twords.adatper.SentenceAdapter;
import com.design.twords.config.ConstantValue;
import com.design.twords.database.Phrase_all;
import com.design.twords.database.RelWord_all;
import com.design.twords.database.Sentence_all;
import com.design.twords.database.Translation_all;
import com.design.twords.database.Word_all;
import com.design.twords.utils.MediaHelper;
import com.design.twords.utils.MyApplication;

import org.litepal.LitePal;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by Alen
 * Time:  17:47
 */
public class WordDetailActivity extends AppCompatActivity {
    private static final String TAG = "WordDetailActivity";

    private TextView wordname, usphone, ukphone, trans, remmethod, remark;
    private RecyclerView worddetail_sents, worddetail_pharses, worddetail_relwords;
    private ImageView collect, worddetail_back, worddetail_song;
    private Button bt_next;
    private LinearLayout bottom_btgroup;
    private ImageView more;

    private CardView remarkarea, remMethodarea, sentsarea, relwordsarea, pharsesarea;

    private SentenceAdapter sentAdapter;
    private PharseAdapter pharseAdapter;
    private RelWordAdapter relWordAdapter;

    private Word_all word_all;

    private String word;
    private static  long wordid;
    private int flags;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_worddetail);
        MyApplication.getInstance().addActivity(this);

        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
        int flags = getIntent().getIntExtra("flags", 0);

        //根据wordId 和wordBook 可以唯一确定一个单词
        String wordId = getIntent().getStringExtra("wordId");
        wordid = Long.parseLong(wordId);
        Log.d(TAG,"wordid = "+wordid);
        String booktype = getIntent().getStringExtra("booktype");

        searchWordDetail(booktype, wordId);
        switch (flags){
            case ConstantValue.LEARN_WORD:
            case ConstantValue.LOOK_WORD:
                bottom_btgroup.setVisibility(View.VISIBLE);
                break;
            default:
                bottom_btgroup.setVisibility(View.GONE);
        }

        bt_next.setOnClickListener(v -> {
            switch (flags){
                case ConstantValue.LEARN_WORD:
                    LearnWordActivity.needUpdate = true;
                    onBackPressed();
                    break;
                case ConstantValue.LOOK_WORD:
                    wordid = wordid + 1;
                    Log.d(TAG,"wordid = " + wordid);
                    searchWordDetail(booktype, (String.valueOf(wordid)));
                    break;
                default:


            }
        });

        more.setOnClickListener(v -> Toast.makeText(WordDetailActivity.this,"暂无i",Toast.LENGTH_LONG).show());


       worddetail_back.setOnClickListener(v -> {
           if (flags == ConstantValue.LEARN_WORD){
               LearnWordActivity.needUpdate = true;
           }
           onBackPressed();
       });

        worddetail_song.setOnClickListener(v -> MediaHelper.play(word));

        collect.setOnClickListener(v -> {
            Word_all wordAll = new Word_all();
            if(word_all.getIsCollected() != 0){
                wordAll.setIsCollected(0);
                collect.setBackgroundResource(R.drawable.icon_collect);
            }else{
                wordAll.setIsCollected(1);
                collect.setBackgroundResource(R.drawable.icon_iscollected);
            }
            wordAll.updateAll("wordId = ? and wordBook = ?", wordId + "", booktype);

        });
    }

    private void searchWordDetail(String booktype, String wordId) {

        List<Word_all> word_detail = LitePal.where("wordId = ? and wordBook = ?", wordId, booktype).find(Word_all.class);
        if (word_detail.size() > 0){
            word_all = word_detail.get(0);
            //设置基本信息
            word = word_all.getWord();
            wordname.setText(word);
            usphone.setText(word_all.getUsPhone());
            ukphone.setText(word_all.getUkPhone());

            if(word_all.getRemMethod() != null){
                remmethod.setText(word_all.getRemMethod());
            }else{
                remMethodarea.setVisibility(View.GONE);
            }

            if(word_all.getRemark() != null){
                remark.setText(word_all.getRemark());
            }else{
                remark.setText("");
                remarkarea.setVisibility(View.GONE);
            }

            if(word_all.getIsCollected() != 0){
                collect.setBackgroundResource(R.drawable.icon_iscollected);
            }else{
                collect.setBackgroundResource(R.drawable.icon_collect);
            }

            //设置翻译
            List<Translation_all> transList = word_all.getTransList();
            trans.setText("");
            for(Translation_all tal : transList){
                if(tal.getWordType() != null)
                    trans.append(tal.getWordType() + ":" + tal.getCHSMeaning()+"\n");
                else
                    trans.append(tal.getCHSMeaning()+"\n");

            }


            //设置例句
            List<Sentence_all> sentencesList = word_all.getSentencesList();
            if(sentencesList.size() > 0){
                worddetail_sents.setLayoutManager(new LinearLayoutManager(WordDetailActivity.this, LinearLayoutManager.VERTICAL, false));
                sentAdapter = new SentenceAdapter(this, sentencesList, R.layout.item_sentence, booktype);
                worddetail_sents.setAdapter(sentAdapter);
            }else{
                sentsarea.setVisibility(View.GONE);
            }

            //设置短语
            List<Phrase_all> phrasesList = word_all.getPhrasesList();
            Log.i("Phrase.size", phrasesList.size()+"");
            if (phrasesList.size() > 0){
                worddetail_pharses.setLayoutManager(new LinearLayoutManager(WordDetailActivity.this, LinearLayoutManager.VERTICAL, false));
                pharseAdapter = new PharseAdapter(this, phrasesList, R.layout.item_pharse, booktype);
                worddetail_pharses.setAdapter(pharseAdapter);
            }else{
                pharsesarea.setVisibility(View.GONE);
            }

            //设置同根词
            List<RelWord_all> relWordList = word_all.getRelWordList();
            Log.i("Relword.size", relWordList.size()+"");
            if(relWordList.size() > 0){
                worddetail_relwords.setLayoutManager(new LinearLayoutManager(WordDetailActivity.this, LinearLayoutManager.VERTICAL, false));
                relWordAdapter = new RelWordAdapter(this, relWordList, R.layout.item_relword, booktype);
                worddetail_relwords.setAdapter(relWordAdapter);
            }else{
                relwordsarea.setVisibility(View.GONE);
            }
        }


    }


    private void initView() {
        wordname = findViewById(R.id.worddetail_wordname);
        usphone = findViewById(R.id.worddetail_usphone);
        ukphone = findViewById(R.id.worddetail_ukphone);
        trans = findViewById(R.id.worddetail_trans);
        remmethod = findViewById(R.id.worddetail_remmethod);
        remark = findViewById(R.id.worddetail_remark);
        collect = findViewById(R.id.worddetail_collect);
        worddetail_back = findViewById(R.id.worddetail_back);
        worddetail_song = findViewById(R.id.worddetail_song);

        remarkarea = findViewById(R.id.worddetail_remarkarea);
        remMethodarea = findViewById(R.id.worddetail_remmethodarea);
        sentsarea = findViewById(R.id.worddetail_sentsarea);
        pharsesarea = findViewById(R.id.worddetail_pharsesarea);
        relwordsarea = findViewById(R.id.worddetail_relwordsarea);

        worddetail_sents = findViewById(R.id.worddetail_sents);
        worddetail_pharses = findViewById(R.id.worddetail_pharses);
        worddetail_relwords = findViewById(R.id.worddetail_relwords);

        bt_next = findViewById(R.id.worddetail_nextbt);
        bottom_btgroup = findViewById(R.id.worddetail_bottombt);

        more = findViewById(R.id.worddetail_more);


    }
}
